﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;


namespace TJBHJJ.Models
{
    public class Home:IDisposable
    {
        IQueryable<WebConfig> WebInfo = null;
        IQueryable<News> news = null;
        IQueryable<Product> products = null;
        IQueryable<Down> downloads = null;
        IQueryable<Job> jobs = null;
        IQueryable<SysConfig> sysinfo = null;
        IQueryable<Friend> friends = null;
        IQueryable<Admin> admin = null;
        IQueryable<Message> msg = null;
        public TJBHJJDBEntities DbInfo = new TJBHJJDBEntities();

        //int newsindex = 10, jobsindex = 5, downsindex = 5, productsindex = 10;
        int pagesize = 0;
        public Home()
        {

            if (!int.TryParse(System.Configuration.ConfigurationManager.AppSettings["PageSize"], out pagesize))
            {
                pagesize = 10;
            }
            //初始化Home页上的显示信息

            WebInfo = from info in DbInfo.WebConfig
                      orderby info.Id
                      select info;
            //newclass = from info in webinfo.NewsClass
            //           select info;
            news = from info in DbInfo.News
                   orderby info.Id descending
                   select info;
            products = from info in DbInfo.Product
                       orderby info.Id descending
                       where info.IsDispaly == true
                       select info;
            jobs = from info in DbInfo.Job
                   orderby info.Id descending
                   select info;
            downloads = from info in DbInfo.Down
                        orderby info.Id descending
                        where info.IsDispaly==true
                        select info;
            sysinfo = from info in DbInfo.SysConfig
                      orderby info.Id
                      select info;
            friends = from info in DbInfo.Friend
                      orderby info.OrderId
                      select info;
            admin = from info in DbInfo.Admin
                    orderby info.Id
                    select info;
            msg = from info in DbInfo.Message
                  orderby info.Isread ascending, info.Id  descending
                  select info;
        }

        public string CompanyName
        {
            get
            {
                return WebInfo == null ? "" : ((WebConfig)WebInfo.Where<WebConfig>(x => x.KeyName == "WebSiteName").FirstOrDefault()).KeyValue;
            }
        }

        public string CompanyTitle
        {
            get
            {
                return WebInfo == null ? "" : ((WebConfig)WebInfo.Where<WebConfig>(x => x.KeyName == "WebTitle").FirstOrDefault()).KeyValue;
            }
        }

        public string WebDescript
        {
            get
            {
                return WebInfo == null ? "" : ((WebConfig)WebInfo.Where<WebConfig>(x => x.KeyName == "WebDescript").FirstOrDefault()).KeyValue;
            }
        }

        public string WebCopyRight
        {
            get
            {
                return WebInfo == null ? "" : ((WebConfig)WebInfo.Where<WebConfig>(x => x.KeyName == "WebCropRight").FirstOrDefault()).KeyValue;
            }
        }

        public string WebQQ
        {
            get
            {
                return WebInfo == null ? "" : ((WebConfig)WebInfo.Where<WebConfig>(x => x.KeyName == "WebQQ").FirstOrDefault()).KeyValue;
            }
        }

        public string WebAddress
        {
            get
            {
                return WebInfo == null ? "" : ((WebConfig)WebInfo.Where<WebConfig>(x => x.KeyName == "WebAddress").FirstOrDefault()).KeyValue;
            }
        }

        public string WebTel
        {
            get
            {
                return WebInfo == null ? "" : ((WebConfig)WebInfo.Where<WebConfig>(x => x.KeyName == "WebTel").FirstOrDefault()).KeyValue;
            }
        }

        public string WebUrl
        {
            get
            {
                return WebInfo == null ? "" : ((WebConfig)WebInfo.Where<WebConfig>(x => x.KeyName == "WebUrl").FirstOrDefault()).KeyValue;
            }
        }
        public string WebKey
        {
            get
            {
                return WebInfo == null ? "" : ((WebConfig)WebInfo.Where<WebConfig>(x => x.KeyName == "WebTitle").FirstOrDefault()).KeyValue;
            }
        }
        public string WebKeyDes
        {
            get
            {
                return WebInfo == null ? "" : ((WebConfig)WebInfo.Where<WebConfig>(x => x.KeyName == "WebDescript").FirstOrDefault()).KeyValue;
            }
        }
        public List<News> News
        {
            get
            {
                return news.ToList();
            }
        }
        public List<Product> Products
        {
            get
            {
                return products.ToList();
            }
        }
        public List<Down> Downloads
        {
            get
            {
                return downloads.ToList();
            }
        }
        public List<Job> Jobs
        {
            get
            {
                return jobs.ToList();
            }
        }
        public List<SysConfig> SysInfo
        {
            get
            {
                return sysinfo.ToList();
            }
        }
        public List<WebConfig> WebConfig
        {
            get
            {
                return WebInfo.ToList();
            }
        }
        public List<Friend> Friends
        {
            get
            {
                return friends.ToList();
            }
        }
        public List<Admin> Admins
        {
            get
            {
                return admin.ToList();
            }
        }
        public List<Message> Msg
        {
            get 
            {
                return msg.ToList();
            }
        }
        public int PageSize
        {
            get
            { return pagesize; }
        }
        /// <summary>
        /// 定义分页的方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="tables"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public List<T> Page<T>(List<T> tables, int page)
        {
            return tables.Skip(page * PageSize).Take(PageSize).ToList();
        }

        //public List<IModelBase> Group(List<IModelBase> tables, int id)
        //{
        //    List<IModelBase> result = new List<IModelBase>();

        //    result.Add(tables.OrderByDescending(x => x.Id).Where(x => x.Id > id).FirstOrDefault());
        //    result.Add(tables.Where(x => x.Id == id).FirstOrDefault());
        //    result.Add(tables.OrderBy(x => x.Id).Where(x => x.Id > id).FirstOrDefault());
        //    return result;
        //}

        public List<News> Group(List<News> tables, int id)
        {
            List<News> result = new List<News>();

            result.Add(tables.OrderBy(x => x.Id).Where(x => x.Id > id).FirstOrDefault());
            result.Add(tables.Where(x => x.Id == id).FirstOrDefault());
            result.Add(tables.OrderByDescending(x => x.Id).Where(x => x.Id < id).FirstOrDefault());
            return result;
        }
        public List<Product> Group(List<Product> tables, int id)
        {
            List<Product> result = new List<Product>();

            result.Add(tables.OrderBy(x => x.Id).Where(x => x.Id > id).FirstOrDefault());
            result.Add(tables.Where(x => x.Id == id).FirstOrDefault());
            result.Add(tables.OrderByDescending(x => x.Id).Where(x => x.Id < id).FirstOrDefault());
            return result;
        }
        public List<Job> Group(List<Job> tables, int id)
        {
            List<Job> result = new List<Job>();

            result.Add(tables.OrderBy(x => x.Id).Where(x => x.Id > id).FirstOrDefault());
            result.Add(tables.Where(x => x.Id == id).FirstOrDefault());
            result.Add(tables.OrderByDescending(x => x.Id).Where(x => x.Id < id).FirstOrDefault());
            return result;
        }
        public List<Down> Group(List<Down> tables, int id)
        {
            List<Down> result = new  List<Down>();

            result.Add(tables.OrderBy(x => x.Id).Where(x => x.Id > id).FirstOrDefault());
            result.Add(tables.Where(x => x.Id == id).FirstOrDefault());
            result.Add(tables.OrderByDescending(x => x.Id).Where(x => x.Id < id).FirstOrDefault());
            return result;
        }

        public void Alert(HttpResponseBase response,string msg)
        {
            response.Write("<script> alert('" + msg + "!');</script>");

        }

        public void Dispose()
        {
            DbInfo.Dispose();
        }


        public string getIconName(string filename)
        {
            if (filename == null || filename == "")
                return "/images/image_51x51.gif";
            else
            {
                filename = filename.ToUpper();
                filename = System.IO.Path.GetExtension(filename);
                Dictionary<string, string> iconlist = new Dictionary<string, string>();
                iconlist.Add(".DOC", "/images/Word.png");
                iconlist.Add(".DOCX", "/images/Word.png");
                iconlist.Add(".XLS", "/images/Excel.png");
                iconlist.Add(".XLSX", "/images/Excel.png");
                iconlist.Add(".PPT", "/images/PowerPoint.png");
                iconlist.Add(".PPTX", "/images/PowerPoint.png");
                iconlist.Add(".PPS", "/images/PowerPoint.png");
                iconlist.Add(".PDF", "/images/PDF.PNG");
                iconlist.Add(".RAR", "/images/RAR.png");
                iconlist.Add(".ZIP", "/images/PowerPoint.png");
                iconlist.Add(".CAB", "/images/PowerPoint.png");
                iconlist.Add(".7Z", "/images/PowerPoint.png");
                iconlist.Add(".ISO", "/images/PowerPoint.png");
                if (iconlist.ContainsKey(filename))
                {
                    return iconlist[filename];
                }
                else
                {
                    return "/images/Other.png";
                }
            }
        }

    }

    public partial class News
    {

        public bool IsReaded(long id)
        {
            using (TJBHJJDBEntities db = new TJBHJJDBEntities())
            {
                News n = (News)(from i in db.News
                                where i.Id == id
                                select i).SingleOrDefault();
                if (n != null)
                {
                    db.News.Detach(n);
                    n.ReadCount += 1;
                    db.News.Attach(n);
                    db.ObjectStateManager.ChangeObjectState(n, System.Data.EntityState.Modified);
                    try
                    {
                        db.SaveChanges();
                        return true;
                    }
                    catch
                    {
                        return false;
                    }
                }
                return false;
            }

        }
    }

    public partial class Product
    {

        public bool IsReaded(long id)
        {
            using (TJBHJJDBEntities db = new TJBHJJDBEntities())
            {
                Product n = (Product)(from i in db.Product
                                      where i.Id == id
                                      select i).SingleOrDefault();
                if (n != null)
                {
                    db.Product.Detach(n);

                    n.ReadCount += 1;
                    db.Product.Attach(n);
                    db.ObjectStateManager.ChangeObjectState(n, System.Data.EntityState.Modified);
                    try
                    {
                        db.SaveChanges();
                        return true;
                    }
                    catch
                    {
                        return false;
                    }
                }
                return false;
            }

        }
    }
    
    public partial class Job
    {

        public bool IsReaded(long id)
        {
            using (TJBHJJDBEntities db = new TJBHJJDBEntities())
            {
                Job n = (Job)(from i in db.Job
                                      where i.Id == id
                                      select i).SingleOrDefault();
                if (n != null)
                {
                    db.Job.Detach(n);

                    n.ReadCount += 1;
                    db.Job.Attach(n);
                    db.ObjectStateManager.ChangeObjectState(n, System.Data.EntityState.Modified);
                    try
                    {
                        db.SaveChanges();
                        return true;
                    }
                    catch
                    {
                        return false;
                    }
                }
                return false;
            }

        }
    }

    public partial class Down
    {

        public bool IsReaded(long id)
        {
            using (TJBHJJDBEntities db = new TJBHJJDBEntities())
            {
                Down n = (Down)(from i in db.Down
                              where i.Id == id
                              select i).SingleOrDefault();
                if (n != null)
                {
                    db.Down.Detach(n);

                    n.ReadCount += 1;
                    db.Down.Attach(n);
                    db.ObjectStateManager.ChangeObjectState(n, System.Data.EntityState.Modified);
                    try
                    {
                        db.SaveChanges();
                        return true;
                    }
                    catch
                    {
                        return false;
                    }
                }
                return false;
            }

        }
    }

    public partial class Message
    {

        public bool IsReaded(long id)
        {
            using (TJBHJJDBEntities db = new TJBHJJDBEntities())
            {
                Message n = (Message)(from i in db.Message
                                where i.Id == id
                                select i).SingleOrDefault();
                if (n != null)
                {
                    db.Message.Detach(n);

                    n.Isread = true;
                    db.Message.Attach(n);
                    db.ObjectStateManager.ChangeObjectState(n, System.Data.EntityState.Modified);
                    try
                    {
                        db.SaveChanges();
                        return true;
                    }
                    catch
                    {
                        return false;
                    }
                }
                return false;
            }

        }
    }
}